namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineFibonacciFanLines : PaintLineBase
    {
        public PaintLineFibonacciFanLines()
        {
            base.LineType = PaintLineType.FibonacciFanLines;
            base.PlName_zh = "斐波那契扇形线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                RectangleF ef = new RectangleF(x - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 1;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else if (ef2.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 2;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                PaintLineBase.PointValue value2 = pList[0];
                float num = (base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value3 = pList[1];
                float num2 = (base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(Color.FromArgb(150, curColor));
                float num5 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num6 = base.CgInstance.GetX(num4) - base.CgInstance.LeftYScaleWidth;
                pen.Width = base.LineWidth;
                pen.DashStyle = DashStyle.Dash;
                g.DrawLine(pen, num5, num, num6, num2);
                pen.Dispose();
                pen = new Pen(curColor) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                if (base.Selected || (num5 == num6))
                {
                    g.FillRectangle(brush, new RectangleF(num5 - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                if ((num5 != num6) && (num != num2))
                {
                    PointF tf = new PointF(num6, num2 - ((num2 - num) * 0.382f));
                    PointF tf2 = new PointF(num6, num2 - ((num2 - num) * 0.5f));
                    PointF tf3 = new PointF(num6, num2 - ((num2 - num) * 0.618f));
                    PointF tf4 = new PointF(num5, num);
                    List<PointF> list = new List<PointF>();
                    PointF[] collection = new PointF[] { tf, tf2, tf3 };
                    list.AddRange(collection);
                    for (int i = 0; i < list.Count; i++)
                    {
                        PointF tf5 = list[i];
                        PointF tf6 = list[i];
                        double[] numArray = Common.GetLineParam(tf4.X, tf4.Y, tf5.X, tf6.Y);
                        float workSpaceX = 0f;
                        float num9 = 0f;
                        if (num6 > num5)
                        {
                            num9 = (float) ((numArray[0] * base.CgInstance.GetWorkSpaceX()) + numArray[1]);
                            workSpaceX = base.CgInstance.GetWorkSpaceX();
                        }
                        else
                        {
                            num9 = (float) numArray[1];
                            workSpaceX = 0f;
                        }
                        g.DrawLine(pen, tf4.X, tf4.Y, workSpaceX, num9);
                    }
                }
                brush.Dispose();
                pen.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                if ((mouseOverIndex >= 0) && (mouseOverIndex <= (base.CgInstance.LastVisibleRecord - 1)))
                {
                    ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                    DataRow row = base.CgInstance.DataSource.Rows[mouseOverIndex];
                    DataRow row2 = base.CgInstance.DataSource.Rows[mouseOverIndex];
                    string key = row[base.CgInstance.CalenderScaleField].ToString();
                    string str2 = row2[base.CgInstance.CalenderScaleField].ToString();
                    base.PointList.Clear();
                    double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                    base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                    base.PointList.Add(new PaintLineBase.PointValue(1, str2, valueByPoint));
                    return true;
                }
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                PaintLineBase.PointValue value2 = base.PointList[0];
                float y = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                RectangleF ef = new RectangleF(x - (base.LineWidth * 2.5f), y - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint) || ef2.Contains((PointF) crossHairPoint))
                {
                    return true;
                }
                PointF tf = new PointF(num6, num2 - ((num2 - y) * 0.382f));
                PointF tf2 = new PointF(num6, num2 - ((num2 - y) * 0.5f));
                PointF tf3 = new PointF(num6, num2 - ((num2 - y) * 0.618f));
                PointF tf4 = new PointF(x, y);
                double[] numArray = Common.GetLineParam(x, y, num6, num2);
                float num7 = (x <= num6) ? x : num6;
                float num8 = (y <= num2) ? y : num2;
                float num9 = (x > num6) ? x : num6;
                float num10 = (y > num2) ? y : num2;
                if (((crossHairPoint.X >= (num7 - (base.LineWidth * 2))) && (crossHairPoint.X <= (num9 + (base.LineWidth * 2)))) && ((crossHairPoint.Y >= (num8 - (base.LineWidth * 2))) && (crossHairPoint.Y <= (num10 + (base.LineWidth * 2)))))
                {
                    if (numArray == null)
                    {
                        return true;
                    }
                    if (((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray[0]) + numArray[1])) >= 0.95) && ((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray[0]) + numArray[1])) <= 1.05))
                    {
                        return true;
                    }
                }
                List<PointF> list = new List<PointF>();
                PointF[] collection = new PointF[] { tf, tf2, tf3 };
                list.AddRange(collection);
                if (((num6 > x) && (crossHairPoint.X >= (x - 2f))) || ((crossHairPoint.X <= (x + 2f)) && (num6 < x)))
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        PointF tf5 = list[i];
                        PointF tf6 = list[i];
                        double[] numArray2 = Common.GetLineParam(tf4.X, tf4.Y, tf5.X, tf6.Y);
                        if (((numArray2 != null) && ((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray2[0]) + numArray2[1])) >= 0.95)) && ((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray2[0]) + numArray2[1])) <= 1.05))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num3 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num6 = (num3 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num5 - num4);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num8 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num9 = (base.CgInstance.GetRecordByTime(value2.Key) + (num8 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num10 = (base.CgInstance.GetRecordByTime(value3.Key) + (num8 - indexByPoint)) - 1;
                            if (num9 >= 0)
                            {
                                if (num9 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num9 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num9 = 0;
                            }
                            if (num10 < 0)
                            {
                                num10 = 0;
                            }
                            else if (num10 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num10 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num6);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num10][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num6);
                            return;
                        }
                        case 1:
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;

                        case 2:
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            return;
                    }
                }
            }
        }
    }
}

